//
//  MyOrderMenuManagement.m
//  X-Touch2
//
//  Created by shengchao yang on 12-4-15.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "MyOrderMenuManagement.h"
#import "Singleton.h"
#import "Imports2.h"
#import "Common.h"
#import "FileManager.h"

@interface MyOrderMenuManagement ()

@end

@implementation MyOrderMenuManagement

- (id)initWithNibName:(NSString *)nibNameOrNil withObj:(id)obj{
    funcType = (NSString *)obj;
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {
        [AppSession initPluTable];//初始化桌号
        [AppSession initPluEmpno];//初始化工号
        
        ((UILabel *)[self.view viewWithTag:TagOfUILabel]).text = [NSString stringWithFormat:@"%@：%@",TranslateStr(@"主单号"), AppSession.currentOrderMenu.momLocalMenu];
        ((UITextField *)[self.view viewWithTag:TagOfUITextField]).text = AppSession.currentOrderMenu.momDescript;
        
        //初始化pickerview
        _picker = (UIPickerView *)[self.view viewWithTag:TagOfUIPickerView];
        NSInteger row1 = 0;
        for (int i=0; i<AppSession.pluTables.count; i++) {
            PluTable *temp = (PluTable *)[AppSession.pluTables objectAtIndex:i];
            if ([temp.ptTableNo isEqualToString:AppSession.currentOrderMenu.ptTableNo]) {
                row1 = i;
                break;
            }
        }
        NSInteger row2 = [AppSession.currentOrderMenu.persons intValue];
        NSInteger row3 = 0;
        for (int i=0; i<AppSession.pluEmpnos.count; i++) {
            PluEmpno * temp = (PluEmpno *)[AppSession.pluEmpnos objectAtIndex:i];
            if ([temp.peEmpno isEqualToString:AppSession.currentOrderMenu.peEmpno]) {
                row3 = i;
                break;
            }
        }
        MyNSLog(@"[%d,%d,%d]", row1,row2,row3);
        [_picker selectRow:row1 inComponent:0 animated:NO];
        [_picker selectRow:row2 inComponent:1 animated:NO];
        [_picker selectRow:row3 inComponent:2 animated:NO];
        
        [((UIButton *)[self.view viewWithTag:TagOfUIButton]) setTitle:TranslateStr(@"确定") forState:UIControlStateNormal];
        [((UIButton *)[self.view viewWithTag:TagOfUIButton+1]) setTitle:TranslateStr(@"返回") forState:UIControlStateNormal];
    }
    return self;
}

- (IBAction)buttonClicked:(id)sender{
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
    if (bid == TagOfUIButton) {
        MyNSLog(@"点击确定按钮");
        //保存主单信息
        NSInteger tableNoIndex = [_picker selectedRowInComponent:0];
        PluTable * tableObj = (PluTable *)[AppSession.pluTables objectAtIndex:tableNoIndex];
        NSInteger persons = [_picker selectedRowInComponent:1];
        NSInteger empnoIndex = [_picker selectedRowInComponent:2];
        PluEmpno * empnoObj = (PluEmpno *)[AppSession.pluEmpnos objectAtIndex:empnoIndex];
        
        if ([funcType isEqualToString:@"login"]) {//不改momType
            NSString * momDescript = ((UITextField *)[self.view viewWithTag:TagOfUITextField]).text;
            NSString * updateSql = [NSString stringWithFormat:@"update MyOrderMenus set ptTableNo='%@',ptDescripts='%@',persons='%d',peEmpno='%@',momTime='%@',momType='%@',momDescript='%@' where momLocalMenu='%@'",tableObj.ptTableNo,tableObj.ptDescripts, persons,empnoObj.peEmpno,CurrentTime,AppSession.currentOrderMenu.momType,momDescript,AppSession.currentOrderMenu.momLocalMenu];
            [DataBaseAccess Update:updateSql];
            [AppSession getCurrentOrderMenu:AppSession.currentOrderMenu.momLocalMenu];//重新初始化主单对象
            postN(@"SysMsg_GuestOrdering_refreshToolBar");      
            postN(@"SysMsg_MyOrderView_refreshToolBar");  
            NSString *SysMsg = [NSString stringWithFormat:@"SysMsg_MessageBoxBaseView_%@", self.nibName];
            postN(SysMsg);//关闭弹出窗口
        }else {
            NSString * momDescript = ((UITextField *)[self.view viewWithTag:TagOfUITextField]).text;
            NSString * updateSql = [NSString stringWithFormat:@"update MyOrderMenus set ptTableNo='%@',ptDescripts='%@',persons='%d',peEmpno='%@',momTime='%@',momType='1',momDescript='%@' where momLocalMenu='%@'",tableObj.ptTableNo,tableObj.ptDescripts, persons,empnoObj.peEmpno,CurrentTime,momDescript,AppSession.currentOrderMenu.momLocalMenu];
            [DataBaseAccess Update:updateSql];
            [AppSession getCurrentOrderMenu:AppSession.currentOrderMenu.momLocalMenu];//重新初始化主单对象
            
            //生成新的主单
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                            message:TranslateStr(@"是否生成新主单")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"取消") 
                                                  otherButtonTitles:TranslateStr(@"确定"), nil];
            alert.tag = TagOfUIAlertView;
            [alert show];
            [alert release];
        }
    }else if(bid == TagOfUIButton + 1){
        MyNSLog(@"返回...");
        NSString *SysMsg = [NSString stringWithFormat:@"SysMsg_MessageBoxBaseView_%@", self.nibName];
        postN(SysMsg);//关闭弹出窗口
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == alertView.firstOtherButtonIndex) {
        [AppSession initCurrentOrderMenu:AppSession.bookIndex];
        [AppSession initMyOrderDishes];
        [AppSession initMyOrderSortedDishes];
        postN(@"SysMsg_MyOrderTableModeView_refreshMyOrderTable"); 
        postN(@"SysMsg_MyOrderNineModeView_refreshNineMode");
    }
    postN(@"SysMsg_GuestOrdering_refreshToolBar");      
    postN(@"SysMsg_MyOrderView_refreshToolBar");  
    NSString *SysMsg = [NSString stringWithFormat:@"SysMsg_MessageBoxBaseView_%@", self.nibName];
    postN(SysMsg);//关闭弹出窗口
}

#pragma mark - Picker dataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {//桌号
        return AppSession.pluTables.count;
    }else if(component == 1){//人数
        return 50;
    }else {//工号
        return AppSession.pluEmpnos.count;
    }
}

#pragma mark - Picker delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) { 
        PluTable * temp = (PluTable *)[AppSession.pluTables objectAtIndex:row];
        NSString * table = [NSString stringWithFormat:@"        %@[%@]",JsonField(temp.ptDescripts), temp.ptTableNo];
        return table;
    }else if(component == 1){
        return [NSString stringWithFormat:@"        %d",row];
    }else{
        PluEmpno * temp = (PluEmpno *)[AppSession.pluEmpnos objectAtIndex:row];
        NSString * empno = [NSString stringWithFormat:@"        %@[%@]",temp.peEmpname, temp.peEmpno];
        return empno;   
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component == 0) {
        return 200;
    }else if(component == 1){
        return 100;
    }else {
        return pickerView.frame.size.width - 200 - 100 - 30;
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
//    NSInteger row1 = [pickerView selectedRowInComponent:0];
//    NSInteger row2 = [pickerView selectedRowInComponent:1];
//    NSInteger row3 = [pickerView selectedRowInComponent:2];
}

- (void)viewDidLoad{
    [super viewDidLoad];
}

- (void)viewDidUnload{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	return YES;
}

@end
